﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace timetable
{
    [Serializable]
    public class Curriculum 
    {
        private static readonly string[] Coursename =
        {
            "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理", "通技"
        };
        public List<List<CourseCell>> table;
        public HashSet<string> CourseSet;
        // 记录每天的每门课的课程数
        public List<Dictionary<string, int>> everydayCourseCount = new List<Dictionary<string, int>>(5);
        public Curriculum(string filePath ="coursetable.txt")
        {
            table = new List<List<CourseCell>>();
            Dictionary<string, int> dayCourseDictionary = Coursename.ToDictionary(s => s, s => 0);
            for (int i = 0; i < 5; i++)
            {
                everydayCourseCount.Add(new Dictionary<string, int>(dayCourseDictionary));
            }
            CourseSet = new HashSet<string>();
            
            GetData(filePath);
        }

        public Curriculum(Curriculum curriculum)
        {
            // TODO: Complete member initialization
            CourseSet = new HashSet<string>(curriculum.CourseSet);
            
            table = new List<List<CourseCell>>();
            foreach (List<CourseCell> cells in curriculum.table)
            {
                var day = new List<CourseCell>();
                foreach (CourseCell cell in cells)
                {
                    day.Add(new CourseCell(cell));
                }
                table.Add(day);
            }
            Dictionary<string, int> dayCourseDictionary = Coursename.ToDictionary(s => s, s => 1);
            for (int i = 0; i < 5; i++)
            {
                everydayCourseCount.Add(new Dictionary<string, int>(dayCourseDictionary));
            }
        }

        /// <summary>
        /// 读取课程模板
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public void GetData(string filePath)
        {
            string line;
            string[] stringArray;
            char[] charArray = { '\t' };

            try
            {
                FileStream aFile = new FileStream(filePath, FileMode.Open);
                StreamReader sr = new StreamReader(aFile);

                line = sr.ReadLine();
                while (line != null)
                {
                    stringArray = line.Split(charArray);
                    List<CourseCell> dataRow = new List<CourseCell>();

                    for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
                    {
                        CourseCell course = new CourseCell();
                        if (stringArray[x].Length > 1)
                        {
                            course.CourseName = stringArray[x].Substring(0, 2);
                            course.PositionClass = int.Parse(stringArray[x].Substring(2, 1));
                        }
                        else course.CourseName = stringArray[x];

                       dataRow.Add(course);

                    }

                    table.Add(dataRow);

                    line = sr.ReadLine();
                }

                sr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }


        // 分班的同时将课程名称格式化
        public void changeCourseName(string oldCourseName, string newCourseName, bool isSpecail = false)
        {
            foreach (List<CourseCell> cells in table)
            {
                foreach (var cell in cells)
                {
                    if (cell.CourseName == oldCourseName)
                    {
                        
                        cell.CourseName = newCourseName;
                        cell.isSpecialCourse = isSpecail ? 1 : 0;
                    }

                }
            }
            if(newCourseName != null)
                CourseSet.Add(newCourseName);
        }

        //更新课程数量
        public void RefreshEverydayCourseCount()
        {
            for (int i = 0; i < 5; i++)
            {
                foreach (CourseCell cell in table[i])
                {
                    if (Coursename.Contains(cell.CourseName))
                    {
                        everydayCourseCount[i][cell.CourseName]++;
                    }
                    
                }
            }
        }
    }
}
